package com.nutdiary.diary.presenter;

import android.support.annotation.NonNull;

import com.nutdiary.diary.base.BasePresenter;
import com.nutdiary.diary.bean.MainListBean;
import com.nutdiary.diary.bean.MainListItem;
import com.nutdiary.diary.bean.UploadBean;
import com.nutdiary.diary.contract.MainContract;
import com.nutdiary.diary.model.MainModel;
import com.nutdiary.diary.network.FileUploadObserver;
import com.nutdiary.diary.network.UploadFileRequestBody;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.io.File;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class MainPresenter extends BasePresenter {
    private MainContract.MainView mainView;
    private MainContract.MainModel mainModel;

    public MainPresenter(MainContract.MainView mainView, LifecycleProvider<ActivityEvent> provider) {
        super(provider);
        this.mainView = mainView;
        mainModel = new MainModel();
    }

    /**
     */
    public void getListData(String token) {
        mainModel.getMainListData(token)
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .compose(getProvider().<MainListBean>bindUntilEvent(ActivityEvent.DESTROY))// onDestroy取消订阅
                .subscribe(new DefaultObserver<MainListBean>() {  // 订阅
                    @Override
                    public void onNext(@NonNull MainListBean mainListBean) {
                        mainView.showToast(mainListBean.getMsg());
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mainView.showToast(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        mainView.showToast("onComplete");
                    }
                });
    }

    public void saveItemData(MainListItem mainListItem) {
        mainModel.saveItemData(mainListItem)
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .compose(getProvider().<MainListBean>bindUntilEvent(ActivityEvent.DESTROY))// onDestroy取消订阅
                .subscribe(new DefaultObserver<MainListBean>() {  // 订阅
                    @Override
                    public void onNext(@NonNull MainListBean mainListBean) {
                        mainView.showToast(mainListBean.getMsg());
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mainView.showToast(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        mainView.showToast("onComplete");
                    }
                });
    }

    public void uploadFile(File file) {

        mainView.showDialog();
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("application/otcet-stream"), file);

        FileUploadObserver<UploadBean> fileUploadObserver = new FileUploadObserver<UploadBean>() {
            @Override
            public void onUpLoadSuccess(UploadBean responseBody) {
                //上传成功
                mainView.dissmissDialog();

            }

            @Override
            public void onUpLoadFail(Throwable e) {
                //上传失败
                mainView.dissmissDialog();
            }

            @Override
            public void onProgress(int progress) {
                //progress 0-100
               // dialog.setProgress(progress);
                System.out.println(progress+">>>>>>>>>>");
                mainView.showProgress(progress);
            }
        };

        UploadFileRequestBody uploadFileRequestBody = new UploadFileRequestBody(file,fileUploadObserver);

        MultipartBody.Part body =
                MultipartBody.Part.createFormData("file", file.getName(), uploadFileRequestBody);


        mainModel.uploadFile(body)
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .compose(getProvider().<UploadBean>bindUntilEvent(ActivityEvent.DESTROY))// onDestroy取消订阅
                .subscribe(fileUploadObserver);
    }

}
